#!/bin/bash

SUBJECTS_DIR="/media/amd/data/long/mdd_3d_constr"
SAV_DIR="/media/amd/data/long/mdd_3d_constr_dk68"
MAX_JOBS=50
CSV_FILE="pat_effi_recon_all.csv"

process_subject() {
    local subj=\$1
    local lh_path="$SAV_DIR/${subj}.dk.stats.lh.table"
    local rh_path="$SAV_DIR/${subj}.dk.stats.rh.table"

    if [[ -f "$lh_path" ]]; then
        mris_anatomical_stats -a "$SUBJECTS_DIR/$subj/label/lh.aparc.annot" \
                             -b -f "$lh_path" "$subj" lh
    else
        echo "WARNING: $lh_path not found for $subj (LH)"
    fi

    if [[ -f "$rh_path" ]]; then
        mris_anatomical_stats -a "$SUBJECTS_DIR/$subj/label/rh.aparc.annot" \
                             -b -f "$rh_path" "$subj" rh
    else
        echo "WARNING: $rh_path not found for $subj (RH)"
    fi
}

export -f process_subject
export SUBJECTS_DIR
export SAV_DIR

# 读取CSV文件并获取第一列的所有sub（跳过表头）
tail -n +2 "$CSV_FILE" | cut -d ',' -f 1 | parallel -j $MAX_JOBS --progress --joblog parallel_joblog.txt \
    "process_subject {}"

echo "All tasks have been completed - see parallel_joblog.txt"
